# -*- coding: utf-8 -*-
# @Time : 2024/2/1 15:07
# @Author : ZH
# @File : 24.py
# @Software: PyCharm
# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next




class Solution(object):
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        res = ListNode(-1, head)
        c = ListNode(-1, res)
        r = res.next
        while r and r.next:
            # 创建临时变量
            # h = ListNode(-1, )
            t = ListNode(-1, r.next.next)
            i = ListNode(-1, r)
            j = ListNode(-1, r.next)
            c.next.next = j.next
            c = ListNode(-1, r)
            # print(i.next.val if i.next else None, j.next.val if j.next else None, t.next.val if t.next else None)
            i.next.next = t.next
            j.next.next = i.next
            # r.next = i.next
            # r.next = j.next
            r = t.next

            # print(r.val if r else None)
        return res.next

if __name__ == '__main__':
    # [1,2,3,4]
    so = Solution()
    header = ListNode(1, ListNode(2, ListNode(3, ListNode(4))))
    z = so.swapPairs(header)
    while z:
        print(z.val)
        z = z.next
    # print(z)

